﻿Imports System.Data.OleDb
Public Class _Default

    Inherits Page
    Public odConnection As OleDbConnection = New OleDbConnection()

    Private Function Accsu(ByVal b As String, ByVal l As String, ByVal n As String， ByVal cl As String) As String
        Dim z As String
        '定义一个OLEDB连接并实例化它
        Dim con As New OleDbConnection
        '定义一个OLEDB命令并实例化他
        Dim cmd As New OleDbCommand
        '定义一个OLEDBReader方法来读取数据库
        Dim dr As OleDbDataReader
        '初始化con的连接属性，使用OLEDB模式，数据源为：你指定下路径，我的是在D盘
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\date\db\db.mdb"
        '打开OLEDB数据连接
        con.Open()
        '初始化OLEDB命令的连接属性为con,这个需要你理解下
        cmd.Connection = con
        '初始化OLEDB命令的语句 就是查询 什么字段从什么表 条件是ID等于你在t1中输入的内容
        cmd.CommandText = "select " & l & " from " & b & " where " & cl & "='" & n & "'"
        '执行OLEDB命令以ExecuteReader()方式，并返回一个OLEDBReader，赋值给dr
        dr = cmd.ExecuteReader()
        '判断下dr中是否有数据。如果有就把第一个值赋值给t2的值
        If dr.Read() Then
            z = dr(0)
            Accsu = z
        Else
            Response.Write("<Script Language=JavaScript>alert('未找到用户');</Script>")
            Accsu = ";"
        End If
        dr.Close()
        con.Close()
        '函数Accsu说明：b为查询的表，l为输出的列，n为查询列的值,cl为查询的列
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    End Sub



    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Me.TextBox1.Text = Nothing Then
            Response.Write("<script type='text/javascript'>alert('禁止，请输入姓名！')</script>")
        Else
            Dim a As String = Accsu("nam", "ID", Me.TextBox1.Text, "nam")
            If a = ";" Then
            Else
                If Accsu("nam", "[all]", Me.TextBox1.Text, "nam") = "6" Then
                    Response.Write("<script type='text/javascript'>alert('你已经填报完成')</script>")
                Else
                    odConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\date\db\db.mdb"

                    odConnection.Open()
                    Dim str1 As New System.Text.StringBuilder
                    str1.Append("UPDATE nam SET nam.[学号] = '")
                    str1.Append(Me.TextBox2.Text & "' WHERE (([ID]=")
                    str1.Append(a)
                    str1.Append("))")
                    Debug.WriteLine("")
                    Debug.WriteLine(str1.ToString)
                    Dim cmdn1 As New OleDb.OleDbCommand(str1.ToString, odConnection)
                    cmdn1.ExecuteNonQuery()
                    odConnection.Close()

                    Session("ID") = a
                    Response.Write("<script>window.location='1.aspx'</script>"）
                End If
            End If
        End If
    End Sub
End Class